package com.sfh.cpc.home.menu;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sfh.cpc.com.BaseAppContent;
import com.sfh.cpc.com.ImageLoaderEngine;
import com.sfh.cpc.com.entity.EUser;
import com.sfh.cpc.com.nav.OnTabReselectListener;
import com.sfh.cpc.com.server.INoteServer;
import com.sfh.cpc.com.widget.VIPAgeUserView;
import com.sfh.cpc.com.widget.WidgetCharmWealth;
import com.sfh.cpc.user.LoginActivity;
import com.sfh.cpc.user.UserInfoActivity;
import com.sfh.cpc.user.model.UserModel;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.event.RxBusEvent;
import com.sfh.lib.exception.HandleException;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;
import com.sfh.lib.rx.IResult;
import com.sfh.lib.ui.AbstractLifecycleFragment;
import com.sfh.lib.ui.dialog.DialogBuilder;

import de.hdodenhof.circleimageview.CircleImageView;
import com.sfh.cpc.R;
/**
 * 功能描述: 我的
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/6
 */
public class UserFragment extends AbstractLifecycleFragment<UserModel> implements OnTabReselectListener{
    private CircleImageView ivHeadr;
    private ImageView ivSex;
    private TextView tvName;
    private WidgetCharmWealth wgCharm;
    private WidgetCharmWealth wgWealth;
    private VIPAgeUserView ivVIP;
    private TextView tvGoInfo;
    private TextView tvNote;
    private TextView tvMass;

    private TextView tv_exit;

    private EUser user;

    @Override
    public int getLayout() {
        return R.layout.user_fragment;
    }

    @Override
    public void initData(View view) {
        ivHeadr = view.findViewById(R.id.ivHeadr);
        ivSex = view.findViewById(R.id.ivSex);
        tvName = view.findViewById(R.id.tvName);
        wgCharm = view.findViewById(R.id.wgCharm);
        wgWealth = view.findViewById(R.id.wgWealth);
        ivVIP = view.findViewById(R.id.ivVIP);
        tvGoInfo = view.findViewById(R.id.tvGoInfo);
        tvNote = view.findViewById(R.id.tv_note);
        tvMass = view.findViewById(R.id.tv_mass);
        tv_exit = view.findViewById(R.id.tv_exit);
        tv_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogBuilder dialogBuilder = new DialogBuilder();
                dialogBuilder.setMessage("确定退出当前账号吗？");
                dialogBuilder.setOkListener(new DialogBuilder.DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogBuilder.DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                      // AppCacheManager.getApplication().onLoseToken("");
                    }
                });
                showDialog(dialogBuilder);
            }
        });
        tvGoInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserModel.login(new IResult<EUser>() {
                    @Override
                    public void onFail(HandleException e) {
                        onLoginToast();
                    }

                    @Override
                    public void onSuccess(EUser eUser) throws Exception {
                        UserInfoActivity.startUI(getContext());
                    }
                });
            }
        });
        tvNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 我的话题
                UserModel.login(new IResult<EUser>() {
                    @Override
                    public void onFail(HandleException e) {
                        onLoginToast();
                    }

                    @Override
                    public void onSuccess(EUser eUser) throws Exception {
                        INoteServer noteServer = AppCacheManager.getCache(INoteServer.class.getSimpleName(), INoteServer.class);
                        if (noteServer != null) {
                            noteServer.openUserNote(getContext(), eUser.getUserId());
                        }
                    }
                });
            }
        });
        tvMass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 我的社区
                UserModel.login(new IResult<EUser>() {
                    @Override
                    public void onFail(HandleException e) {
                        onLoginToast();
                    }

                    @Override
                    public void onSuccess(EUser eUser) throws Exception {
                        INoteServer noteServer = AppCacheManager.getCache(INoteServer.class.getSimpleName(), INoteServer.class);
                        if (noteServer != null) {
                            noteServer.openMyMass(getContext());
                        }
                    }
                });
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        getViewModel().getUser();
    }

    @Override
    public void onTabReselect() {
        getViewModel().getUser();
    }


    @RxBusEvent(from = "登录与注册成功")
    @LiveDataMatch
    public void onUser(EUser user) {
        this.user = user;
        if (user != null) {
            tv_exit.setVisibility(View.VISIBLE);
            ivSex.setImageResource(TextUtils.equals("0", user.getUserSex()) ? R.drawable.tag_female : R.drawable.tag_male);
            ivVIP.showVIP(user);
            wgCharm.setStyleCharm(user.getUserCharm() + "");
            wgWealth.setStyleWealth(user.getUserWealth() + "");
            tvName.setText(user.getUserName());
            new ImageLoaderEngine(ivHeadr,  BaseAppContent.getImageUrl( user.getUserUrl())).placeholder(R.drawable.wait_head_second).into(ivHeadr);
        }else{
            tv_exit.setVisibility(View.GONE);
        }
    }


    @LiveDataMatch
    public void  onLoginToast() {
        tv_exit.setVisibility(View.GONE);

        DialogBuilder dialogBuilder = new DialogBuilder();
        dialogBuilder.setMessage("请登录");
        dialogBuilder.setCancelable(false);
        dialogBuilder.setOkListener(new DialogBuilder.DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogBuilder.DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                LoginActivity.startUI(getContext());
            }
        });
        showDialog(dialogBuilder);
    }


}
